package com.woniu.project.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniu.project.core.dto.ResultDto;
import com.woniu.project.core.exception.ProjectProductException;
import com.woniu.project.dto.productsmalltype.ProdcutSmallTypeSelectDto;
import com.woniu.project.params.productsmalltype.ProdcutSmallTypeAddParam;
import com.woniu.project.params.productsmalltype.ProdcutSmallTypeSelectParam;
import com.woniu.project.params.productsmalltype.ProdcutSmallTypeUpdateParam;
import com.woniu.project.service.IProductSmallTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-10-12
 */
@RestController
@RequestMapping("/productSmallType")
@Api(tags="小类管理")
public class ProductSmallTypeController {
    @Autowired
    private IProductSmallTypeService productSmallTypeService;
    //根据是否有id和name进行不同的分页查询
    @ApiOperation(value="小类查询",notes = "参数均选填")
    @GetMapping("/select")
    public ResultDto select(@Valid @ModelAttribute ProdcutSmallTypeSelectParam prodcutSmallTypeSelectParam) {

        try {
            IPage<ProdcutSmallTypeSelectDto> productBigTypeIpage = productSmallTypeService.select(prodcutSmallTypeSelectParam);
            return ResultDto.success(200, "查询成功", productBigTypeIpage);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "商品小类查询系统异常");
        }

    }

    @ApiOperation(value="新增商品小类",notes = "id不填,其他参数均必填")
    //新增商品小类
    @PostMapping("/addSmallType")
    public ResultDto addSmallType(@Valid @RequestBody ProdcutSmallTypeAddParam prodcutSmallTypeAddParam) {
        try {
            int result = productSmallTypeService.addSmallType(prodcutSmallTypeAddParam);
            if (result == 1) {
                return ResultDto.success(500, "该序号已存在，请重新输入", null);
            }
            return ResultDto.success(200, "新增商品小类成功", null);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "商品小类新增系统异常");
        }
    }
    @ApiOperation(value="修改商品小类",notes = "参数全必填")
    //修改商品小类
    @PutMapping("/updateSmallType")
    public ResultDto updateSmallType(@Valid @RequestBody ProdcutSmallTypeUpdateParam prodcutSmallTypeUpdateParam) {
        try {
            int result = productSmallTypeService.updateSmallType(prodcutSmallTypeUpdateParam);
            if (result == 1) {
                return ResultDto.success(500, "该序号已存在，请重新输入", null);
            }
            return ResultDto.success(200, "修改商品小类成功", null);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "商品小类修改系统异常");
        }
    }
    @ApiOperation(value="根据id删除小类",notes = "id必填")
    //根据id删除小类
    @DeleteMapping("/deleteById/{id}")
    public ResultDto deleteById(@PathVariable Integer id) {
        try {
            productSmallTypeService.deleteById(id);
            return ResultDto.success(200, "删除商品小类成功", null);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "商品小类删除系统异常");
        }
    }
}

